<template>
  <div class="converter">
    <el-form class="form-search" inline>
      <el-form-item label="">
        <div
          style="
            display: flex;
            align-items: center;
            position: relative;
            top: 5px;
          "
        >
          <label class="btn-nav-bg stripe-btn" @click="handleAdd()"
            >新建换流站</label
          >
        </div>
      </el-form-item>
    </el-form>
    <el-row :gutter="24">
      <el-col
        :span="6"
        v-for="(item, index) in converterStationList"
        :key="index"
      >
        <div class="station">
          <img :src="item.modelUrl" />
          <div class="item">
            <p>{{ item.site }}换流站</p>
            <p @click="handleRoute(item, 1)">
              状态评估维护 <i class="el-icon-arrow-right"></i>
            </p>
            <p @click="handleRoute(item, 2)">
              进入管理 <i class="el-icon-arrow-right"></i>
            </p>
          </div>
          <div class="tip">
            <el-dropdown>
              <span class="el-dropdown-link">
                <i class="el-icon-more"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  ><p @click="edit(item)">编辑</p></el-dropdown-item
                >
                <el-dropdown-item
                  ><p @click="handleDelete(item)">删除</p></el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
      </el-col>
    </el-row>
    <pub-dialog
      :title="dialogTitle"
      type="big"
      top="15vh"
      :sureBtnLoading="loading"
      :dialogVisible="dialogVisible"
      dialogVisableRef="dialogVisableRef"
      ref="dialogVisableRef"
      @switchVisable="switchVisable"
      @dialogSubmit="dialogSubmit"
      @dialogCancel="dialogCancel"
      btnType="3"
    >
      <el-form
        class="form-search"
        label-width="120px"
        :rules="rules"
        ref="form"
        :model="formData"
      >
        <el-form-item label="换流站名称" prop="site">
          <el-input v-model="formData.site"></el-input>
        </el-form-item>
        <el-form-item label="模型地址" prop="modelUrl">
          <el-input v-model="formData.modelUrl"></el-input>
        </el-form-item>
        <el-form-item label="封面图标" prop="coverFileId">
          <el-upload
            ref="uploadRef"
            action=""
            :on-change="handleUpload"
            :file-list="fileDTOPics"
            :auto-upload="false"
            list-type="picture-card"
            accept=""
            :on-preview="handlePictureCardPreview"
            :disabled="fileDTOPics.length > 0"
            :on-success="handleSuccess"
            :before-upload="handleBeforeUpload"
          >
            <span>{{ fileDTOPics.length }}</span>
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
    </pub-dialog>
  </div>
</template>

<script>
import Evaluate from "./components/Evaluate/index.vue";
import Manage from "./components/Manage/index.vue";
import {
  getXjSiteQuery,
  xjSiteAdd,
  xjSiteDelete,
  xjSiteUpdate,
  xjSiteDetail,
  sysFileUpload,
  sysFileDelete,
} from "@/api/pointMaintain";
import pubDialog from "@/components/pubDialog.vue";

export default {
  name: "converterStation",
  components: { pubDialog },
  data() {
    return {
      converterStationList: [],
      loading: false,
      dialogVisible: false,
      dialogTitle: "新增换流站",
      formData: {
        site: "",
        modelUrl: "",
        coverFileId: "",
      },
      rules: {},
      fileDTOPics: [],
      uploadOption: {
        action: "/sys-file/upload",
        // headers: { "Access-Token": localStorage.getItem("Access-Token") },
        data: {
          // fileType: "discover",
          // equipId: "",
        },
      },
    };
  },
  created() {
    let basePath =
      process.env.NODE_ENV === "production"
        ? window.productiontIp
        : window.developmentIp;
    this.uploadOption.action = basePath + "sys-file/upload";
    this.getList();
  },
  methods: {
    // 查询列表
    getList() {
      getXjSiteQuery().then((res) => {
        if (res.code == 20000) {
          this.converterStationList = res.data;
        }
      });
    },

    // 新增
    handleAdd() {
      this.dialogVisible = true;
      this.dialogTitle = "新增换流站";
      this.formData = {
        site: "",
        modelUrl: "",
        coverFileId: "",
      };
      this.fileDTOPics = [];
    },

    edit(item) {
      this.dialogVisible = true;
      this.fileDTOPics = [];
      this.dialogTitle = "编辑换流站";
      xjSiteDetail({ id: item.id }).then((res) => {
        if (res.code == 20000) {
          this.formData = res.data;
          this.fileDTOPics.push(res.data.fileDTO);
        }
      });
    },

    handleDelete(item) {
      xjSiteDelete({ id: item.id }).then((res) => {
        if (res.code == 20000) {
          this.getList();
          this.$message.success("删除成功！");
        }
      });
    },

    handleUpload(file, fileList) {
      const formData = new FormData();
      // let filesLists = fileList;
      fileList.forEach((item) => {
        formData.append("file", item.raw);
      });
      sysFileUpload(formData).then((res) => {
        if (res.code == 20000) {
          this.fileDTOPics.push({
            fileId: res.data.fileId,
            fileName: res.data.fileName,
            fileSize: res.data.fileSize,
            url: res.data.url,
          });
        }
      });
    },

    handleBeforeUpload(file) {},

    handleSuccess(res, file) {
      if (res.code == 20000) {
        this.$message.success("上传成功");
        this.fileDTOPics.push({
          fileId: res.data.fileId,
          fileName: res.data.fileName,
          fileSize: res.data.fileSize,
          url:
            process.env.NODE_ENV === "production"
              ? window.productiontIp
              : window.developmentIp + res.data.url,
        });
      }
    },

    handlePictureCardPreview(file) {},

    beforeRemove(file) {},

    handleRemove(file, fileList) {
      if (file.fileId) {
        sysFileDelete({ fileIds: [file.fileId] }).then((res) => {
          if (res.code == 20000) {
            this.$message.success("删除成功");
          }
        });
      }
    },

    dialogSubmit() {
      this.loading = true;
      let params = { ...this.formData };
      params.modelUrl =
        process.env.NODE_ENV === "production"
          ? window.productiontIp
          : window.developmentIp + this.fileDTOPics[0].url;
      params.fileId = this.fileDTOPics[0].fileId;
      let API = params.id ? xjSiteUpdate : xjSiteAdd;
      API(params)
        .then((res) => {
          if (res.code == 20000) {
            this.dialogVisible = false;
            this.getList();
            this.$message.success("保存成功");
            this.loading = false;
          }
        })
        .catch((err) => {
          this.loading = false;
          return err;
        });
    },
    switchVisable(visable, ref) {
      this[ref] = visable;
    },
    dialogCancel() {
      this.dialogVisible = false;
    },
    handleRoute(item, flag) {
      this.$router.push({
        path: flag === 1 ? "/pointMaintain/evaluate" : "/pointMaintain/manage",
        query: {
          siteId: item.id,
          siteName: item.site,
        },
      });
    },
  },
};
</script>
<style scoped lang="less">
::v-deep .form-search .el-form-item {
  margin-bottom: 22px;
}
::v-deep .el-row {
  height: calc(100% - 52px);
}
::v-deep .el-dropdown-menu {
  background: #041a35 !important;
  border: 1px solid #40e5f0 !important;
}
::v-deep .el-upload--picture-card {
  background: transparent;
  border-color: #006686;
}
.converter {
  height: calc(100vh - 160px);
  overflow: hidden;
  .station {
    border-radius: 5px;
    border: 1px solid gray;
    overflow: hidden;
    position: relative;
    .tip {
      position: absolute;
      right: 15px;
      top: 10px;
      z-index: 999;
      cursor: pointer;
      color: #ffffff;
      background: #006686;
      width: 24px;
      height: 24px;
      border-radius: 50%;
      text-align: center;
      ::v-deep .el-dropdown {
        top: 2px;
        z-index: 9999;
        i {
          color: #ffffff;
        }
      }
    }
    img {
      width: 100%;
      height: 300px;
    }
    .item {
      display: flex;
      justify-content: space-between;
      height: 48px;
      line-height: 48px;
      padding: 0 15px;
      p {
        color: #ffffff;
        font-size: 12px;
        &:not(:first-child) {
          cursor: pointer;
          &:hover {
            color: #40e5f0;
          }
        }
      }
    }
  }
}
</style>
